// morevideo.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: -1,
        inde: -1,
        cat_id: 0,
        m_id: 0,
        controls: false,
        videoHidden: true,
        cates: [],
        photoadd: [],
        video_url: '',
        condition: true,
        active: '',
        show: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options)
            // console.log(this.data.items.musicClass.length)
        var that = this;
        that.setData({
            video_url: options.video_url
        })
        var session_key = wx.getStorageSync('session_key');
        wx.request({
                url: 'http://sheying.j8j0.com/api/Upload/add_video',
                data: {
                    session_key: session_key
                },
                success: function(res) {
                    console.log(res)
                    let cates = []
                    let musics = []
                    console.log(res.data.data.cate)
                    var Length = res.data.data.cate.length;
                    console.log(Length)
                    for (var i = 0; i < Length; i++) {
                        cates[i] = res.data.data.cate[i].cat_name
                    }
                    for (var i = 0; i < res.data.data.music.length; i++) {
                        musics[i] = res.data.data.music[i].name
                    }
                    that.setData({
                        cates: cates,
                        musics: musics,
                        cate: res.data.data.cate,
                        music: res.data.data.music
                    })
                }
            })
            // wx.getStorage({
            //     key: 'session_key',
            //     success: function(res1) {
            //         // console.log(res1)
            //         console.log(res1.data)


        //         //传递session_key,请求获取数据，渲染页面

        //         console.log(res1.data)
        //             // wx.request({
        //             //     url: 'http://sheying.j8j0.com/api/Upload/plupload',
        //             //     method: 'POST',
        //             //     header: {
        //             //         'content-type': 'application/x-www-form-urlencoded'
        //             //     },
        //             //     data: {
        //             //         session_key: res1.data
        //             //     },
        //             //     success: function(res) {
        //             //         console.log("",res)
        //             //     }
        //             // })
        //     },
        //     fail: function(res1) {
        //         // console.log(res1)
        //     }
        // });


        // 引入SDK核心类
        var QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');

        // 实例化API核心类
        var demo = new QQMapWX({
            key: 'ALWBZ-6WC34-LGBUK-DBH4M-CXPT3-46FUI' // 必填
        });
        demo.reverseGeocoder({
            location: {
                'latitude': 23.129163,
                'longitude': 113.264435
            },
            get_poi: 1,
            success: function(res) {
                that.setData({
                    addresses: res.result.pois
                })
                console.log('res', res)
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    addVideo: function() {
        var that = this;
        wx.chooseVideo({
            sourceType: ['album', 'camera'],
            //录制视频最大时长
            maxDuration: 60,
            //摄像头
            camera: ['front', 'back'],
            //这里返回的是tempFilePaths并不是tempFilePath
            success: function(res) {
                that.setData({
                    video_url: res.tempFilePath,
                    videoHidden: false,
                })
            },
            fail: function(e) {
                console.log(e)
            }
        })
    },
    onShow: function() {
        // this.addVideo()
        this.dingwei()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    listenerBtnmoreVideo: function() {
        wx.navigateTo({
            url: 'videos/videos'
        })
    },
    listenerBtnOpenImage: function() {
        var that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            //sizeType: ['original','compressed'],
            sourceType: ['album'],
            success: function(res) {
                console.log(res)
                    // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths[0];
                // that.photoadd.push(tempFilePaths);
                var photo = that.data.photoadd = tempFilePaths;

                app.globalData.photo_loca = photo;
                that.setData({
                    photoadd: photo
                })
            }
        })
    },

    //获取本地视频路径
    listenerBtnOpenVideo: function() {
        var that = this;
        wx.chooseVideo({
            //相机和相册
            sourceType: ['album', 'camera'],
            //录制视频最大时长
            maxDuration: 60,
            //摄像头
            camera: ['front', 'back'],
            //这里返回的是tempFilePaths并不是tempFilePath
            success: function(res) {
                that.setData({
                    video_url: res.tempFilePath,
                    videoHidden: false,
                })
            },
            fail: function(e) {
                console.log(e)
            }
        })
    },
    // 视频id获取
    bindPickerChangeCat: function(e) {
        var that = this
        console.log('Cat发送选择改变，携带值为', e.detail.value)
        this.setData({
            index: e.detail.value,
            cat_id: that.data.cate[e.detail.value].id
        })
    },
    //音乐id获取
    bindPickerChangeM: function(e) {
        var that = this
        console.log('M发送选择改变，携带值为', e.detail.value)
        this.setData({
            inde: e.detail.value,
            m_id: that.data.cate[e.detail.value].id
        })
    },
    //输入视频名字的时候获取名字
    bindvideo_Text: function(e) {
        this.setData({
            video_Text: e.detail.value
        })
    },

    //输入视频标题的时候获取标题
    //上传视频
    bindpost_video: function() {
        // let session_key = '';
        var that = this;
        console.log(that.data.video_url)
            // if (!that.data.video_url) {
            //     wx.showToast({
            //         title: '请选择视频'
            //     })
            //     return
            // }
        var session_key = wx.getStorageSync('session_key');
        wx.uploadFile({
            url: 'http://sheying.j8j0.com/api/Upload/post_video',
            method: "POST",
            filePath: that.data.video_url,
            name: 'file',
            header: {
                'content-type': 'multipart/form-data'
            },
            formData: {
                'session_key': wx.getStorageSync('session_key'),
                'video_url': that.data.video_url,
                'cat_id': that.data.cat_id,
                'm_id': that.data.m_id,
                'details': that.data.video_Text
            },
            success: function(res) {
                console.log(res)

                var arr = res.data.split(",")
                console.log(arr[1])
                var title = arr[1].substring(10)
                title = title.substring(1);
                title = title.slice(0, -1)
                console.log(title)
                wx.showToast({
                    title: title
                })
                wx.switchTab({
                    url: '../../finds/finds'
                })
            },
            file: function(res) {
                console.log(res)
                wx.showToast({
                    title: res.data
                })
            }
        })

    },
    dingwei: function() {
        var that = this

        wx.getLocation({
            type: 'wgs84',
            success: function(res) {
                console.log(res)
                var latitude = res.latitude;
                var longitude = res.longitude;
                var speed = res.speed;
                var accuracy = res.accuracy;　
                // wx.request({　
                //     url: 'http://api.map.baidu.com/geocoder/v2/?ak=btsVVWf0TM1zUBEbzFz6QqWF&callback=renderReverse&location=' + res.latitude + ',' + res.longitude + '&output=json&pois=1',
                //     data: {},
                //     header: { 'Content-Type': 'application/json' },
                //     success: function(ops) {
                //         console.log('地址', ops.data)　
                //     }　　　　
                // })
                wx.request({
                    url: 'http://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=HLEBZ-6KYC4-KREUS-XHUJE-V5ZH5-3HBJ4',
                    data: {},
                    header: { 'Content-Type': 'application/json' },
                    success: function(ops) {
                        console.log('dizhi2', ops.data)　
                        that.setData({
                            // address: ops.data.result.address,
                            // address: ops.data.result.address_component.city + '·' + ops.data.result.address_component.district
                        })
                    }　　　　
                })
            },
            fail: function() {
                that.dingwei()
            }
        })

    },
    bindGuanbi: function() {
        console.log('aaa')
        this.setData({
            active: '',
            condition: false

        })
    },
    bindAddress: function() {
        this.setData({
            active: 'active',
            show: false
        })
    },
    checkAddress: function(e) {
        console.log(e)
        console.log(e.currentTarget.dataset.value)
        this.setData({
            active: '',
            address: e.currentTarget.dataset.value,
            condition: true,
            show: true
        })
    }
})